package com.menker.app.ui.main;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.github.sahasbhop.apngview.ApngImageLoader;
import com.menker.app.Constants;
import com.menker.app.R;
import com.menker.app.biz.WebPageInfo;
import com.menker.app.common.BaseWebViewToolbarFragment;
import com.menker.app.common.Utils;
import com.menker.app.util.IntentUtils;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.wang.avi.AVLoadingIndicatorView;


import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.FragmentArg;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.res.StringRes;


import static com.menker.app.Constants.API.ENDPOINT;

/**
 * Created by developer on 17-11-20.
 */

@EFragment
public class ListFragment extends BaseWebViewToolbarFragment {

    @FragmentArg("webPageInfo")
    WebPageInfo webPageInfo;

    @StringRes(R.string.error_no_internet)
    protected String strNoInternet;

    private View mLayoutView;

    private AVLoadingIndicatorView mLoadingView;
    private RelativeLayout mRelativeLayout;
    private ImageView mImageAct;

    private final String DEFAULT_URL = ENDPOINT + "App_Content/views/event/index.html";

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mLayoutView = inflater.inflate(R.layout.frag_list, container, false);

        setupUI(webPageInfo, ((MainActivity_) getActivity()).getApiHelper(), ((MainActivity_) getActivity()).getCurrentUser());

        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT
        );
        mLayoutWebView.addView(mWebView, layoutParams);

        return mLayoutView;
    }

    @AfterViews
    void init() {
        delayToHideLoadingView();
    }

    @Override
    protected void findViews() {
        mToolbar = (Toolbar) mLayoutView.findViewById(R.id.frag_list_toolbar);
        mToolbarTitle = (TextView) mLayoutView.findViewById(R.id.frag_list_tv_toolbar_title);
        mToolbarBottomLine = mLayoutView.findViewById(R.id.frag_list_view_toolbar_bottom_line);
        mRefreshLayout = (RefreshLayout) mLayoutView.findViewById(R.id.frag_list_refresh_layout);
        mLayoutWebView = (LinearLayout) mLayoutView.findViewById(R.id.frag_list_layout);
        mImageAct = (ImageView) mLayoutView.findViewById(R.id.search_giv_act);

        mLoadingView = (AVLoadingIndicatorView) mLayoutView.findViewById(R.id.frag_list_loading);
        mRelativeLayout = (RelativeLayout) mLayoutView.findViewById(R.id.frag_list_rlayout);

        String uri = "assets://apng/btn_pt_no.png";
        ApngImageLoader.getInstance().displayApng(uri, mImageAct,
                new ApngImageLoader.ApngConfig(0, true));

        mImageAct.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                IntentUtils.toNewGeneralWebViewActivity(ListFragment.this, new WebPageInfo("", DEFAULT_URL));
            }
        });

    }

    @Background(delay = 2000)
    public void delayToHideLoadingView() {
        hideLoadingView();
    }

    @UiThread
    void hideLoadingView() {
        if (mRelativeLayout != null && mLoadingView != null) {
            mRelativeLayout.setVisibility(View.VISIBLE);
            mLoadingView.setVisibility(View.GONE);
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (resultCode) {
            case Constants.ACTIVITY_CODE.RESULT_REFRESH:
                if (Utils.Network.isNetworkAvailable()) {
                    mWebViewManager.reload();
                } else {
                    Utils.UI.showToastLong(strNoInternet);
                }
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Click(R.id.search_iv_page)
    void toSearchPage() {
        if (Utils.UI.isFastDoubleClick()) {
            return;
        }
        String url = ENDPOINT + "/App_Content/views/home/search2.html";
        WebPageInfo webPageInfo = new WebPageInfo("", url);
        IntentUtils.toNewGeneralWebViewActivity(this, webPageInfo);
    }

    @Click(R.id.search_iv_list)
    void toListPage() {
        if (Utils.UI.isFastDoubleClick()) {
            return;
        }
        String url = ENDPOINT + "/App_Content/views/product/cate.html";
        WebPageInfo webPageInfo = new WebPageInfo("", url);
        IntentUtils.toNewGeneralWebViewActivity(this, webPageInfo);
    }

    public void notifySearchView(int num, final String url) {

        String uri;
        if (num == 0) {
            uri = "assets://apng/btn_pt_no.png";
        }else {
            uri = "assets://apng/btn_pt_have.png";
        }

        ApngImageLoader.getInstance().displayApng(uri, mImageAct,
                new ApngImageLoader.ApngConfig(0, true));

        mImageAct.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                IntentUtils.toNewGeneralWebViewActivity(ListFragment.this,new WebPageInfo("", TextUtils.isEmpty(url) ? DEFAULT_URL : url));
            }
        });

    }


}
